package cn.mxz.generator;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import lincen.javase.util.string.StringUtil;
import cn.mxz.generator.base.GeneratorImpl;
import cn.mxz.generator.config.FileUtil;
import cn.mxz.generator.config.GeneratorConfig;
import cn.mxz.generator.config.IClass;
import cn.mxz.generator.config.IField;
import cn.mxz.generator.config.IMethod;
import cn.mxz.generator.config.IReturn;

public class ASEventsGenerator extends GeneratorImpl{

	private static final String TAIL = "Event";

	public ASEventsGenerator(GeneratorConfig config) {
		super(config, "client_src.events");
	}
	
	@Override
	public void generator() {
		List<IClass> all = config.getClazzs();
		for (IClass c : all) {
			for (IMethod m : c.getMethods()) {
				writeToFile(c, m, config);
			}
		}
	}
	
	private void writeToFile(IClass c, IMethod m, GeneratorConfig config) {
		FileUtil fu = config.getFileUtil(getSrcPath() + File.separator + buildClassName(c, m) + ".as");
		
		String content = buildContent(c, m, config);
		
		fu.writeToFile(content);	
	}

	
	public static final String buildClassName(IClass c, IMethod m) {
		return c.getInterface().getName() + StringUtil.firstToUpperCase(m.getName()) + TAIL;
	}

	private String buildContent(IClass c, IMethod m, GeneratorConfig config) {
		String content = config.getTemplet("ASEvent");
		content = content.replaceAll("PACKAGE_NAME", getPackageName());
		
		content = content.replace("CLASS_DOC", m.getMethodDoc());
		content = content.replace("CLASS_NAME", buildClassName(c, m));
		content = content.replace("EVENT_VALUE", StringUtil.firstToUpperCase(c.getInterface().getName() + "." + m.getName()));
		
		content = content.replace("FILEDS", buildFields(m));
		content = content.replace("GETTER_SETTER", buildGetterSetter(m));
		content = content.replace("CON_ARGS", buildConstentArgs(m));
		content = content.replace("SET_VALUES", buildSetValue(m));
		content = content.replaceAll("IMPORTS", buildImports(c));
		
		return content;
	}
	
	private CharSequence buildSetValue(IMethod m) {
		List<IField> args = m.getArgs();
		Iterator<IField> it = args.iterator();
		
		StringBuilder sb = new StringBuilder();
		
		IReturn rt = m.getReturn();
		if(!rt.isVoid()) {
			sb.append("			this._back = back;\r");
		}
		
		return sb + "";
	}

	private String buildConstentArgs(IMethod m) {
		List<IField> args = m.getArgs();
		Iterator<IField> it = args.iterator();
		
		StringBuilder sb = new StringBuilder();
	
		IReturn rt = m.getReturn();
		if(!rt.isVoid()) {
			sb.append("back:" + rt.getTypeSimple());
		}
		
		return sb + "";
	}

	private String buildGetterSetter(IMethod m) {
		StringBuilder sb = new StringBuilder();
		
		
		IReturn rt = m.getReturn();
		if(!rt.isVoid()) {
			
			String doc = rt.getReturnDoc();

			sb.append("		/**\r");
			sb.append("		 * " + doc + "\r");
			sb.append("		 */\r");
			sb.append("		public function get back ():" + rt.getTypeSimple() + "\r");
			sb.append("		{\r");
			sb.append("			return this._back;\r");
			sb.append("		}\r");
			

//			sb.append("		public function set " + f.getName() + "(value:" + f.getType() + "):void\r");
//			sb.append("		{\r");
//			sb.append("			this._" + f.getName() + " = value;\r");
//			sb.append("		}\r");
		}
		
		for (IField f : m.getArgs()) {
			
		}
		
		return sb + "";
	}

	private String buildFields(IMethod m) {
		StringBuilder sb = new StringBuilder();
		IReturn rt = m.getReturn();
		if(!rt.isVoid()) {
			sb.append("		private var _back:" + rt.getTypeSimple() + ";\r");
		}
		
		return sb + "";
	}
}
